import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import {path} from "./path";
import user from "@/api/user";

const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    ...path
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    const that = Vue.prototype
    if(localStorage.getItem('banma-user')){
        user.getInfo(localStorage.getItem('banma-user'))
            .then((res) => {
                if(res.code === 200) {
                    that.userInfo = res.data
                }else {
                    that.$error(res.msg)
                    that.userInfo = {}
                }
                next()
            })
            .catch((err) => {
                that.$error(err)
                next()
            })
    } else {
        that.userInfo = {}
        next()
    }
})

export default router